#include "Transmitter.h"
#include "GameRes/ParticleData.h"
#include "ParticleMgr.h"
#include "Game/Game.h"
#include "Obj/ObjActor.h"
namespace LTGame
{
	CTransmitter*CTransmitter::curTransmitter = NULL;
	enum{
		ST_NONE,
		ST_FIRE
	};

	CTransmitter::CTransmitter()
	{

	}
	CTransmitter::~CTransmitter()
	{
		if (curTransmitter != NULL){
			curTransmitter->owner = NULL;
			ARRAY_FREE(curTransmitter->timer);
			CC_SAFE_DELETE(curTransmitter);
		}
	}
	CTransmitter*CTransmitter::getInstance()
	{
		if (curTransmitter==NULL)
		{
			curTransmitter = new CTransmitter();
		}
		return curTransmitter;
	}
	void CTransmitter::paint()
	{

	}
	void CTransmitter::logic()
	{
		Transmitter_Logic();
	}
	void CTransmitter::create(int id, XOBJ* actor)
	{
		Transmitter_Create(id, actor);
	}
	void CTransmitter::SetState(int nst)
	{
		if ( state != nst){
			state = nst;
		}
	}
	void CTransmitter::Transmitter_Create(int id,XOBJ* actor)
	{
		transmitter_ID = id;
		owner = actor;
		state = ST_NONE;
		ARRAY_NEW(timer,short, ARRAY_LENGTH(CCGame->m_ParticleData->transmitterGroup[id]));
	}
	void CTransmitter::Transmitter_Logic()
	{
		int i, id;

		switch (state) 
		{
		case ST_NONE:
			for ( i = ARRAY_LENGTH(timer) - 1; i >= 0; i--)
			{
				if (timer[i] <= 0) {
					if (CCGame->useInvincible)
					{
						if (CCGame->wudihuzhuang == false)
						{
							id = CCGame->m_ParticleData->transmitterGroup[28][0][TRANSMITTER_BULLET_GROUP_ID];
							CParticleList::getInstance()->creat(owner,id);
							timer[i] = CCGame->m_ParticleData->transmitterGroup[28][0][TRANSMITTER_BULLET_FREQUENCY];
							SetState(ST_FIRE);
							state_fire_timer = 0;
						}
						break;
					}
					if (CCGame->useBreak)
					{
						switch(CCGame->hero_animationID)
						{
						case ANI_HERO1_ID:
							{
								id = CCGame->m_ParticleData->transmitterGroup[12][i][TRANSMITTER_BULLET_GROUP_ID];
							}
							break;
						case ANI_HERO2_ID:
							{
								id = CCGame->m_ParticleData->transmitterGroup[13][i][TRANSMITTER_BULLET_GROUP_ID];
							}
							break;
						case ANI_HERO3_ID:
							{
								id = CCGame->m_ParticleData->transmitterGroup[14][i][TRANSMITTER_BULLET_GROUP_ID];
							}
							break;
						}
						CParticleList::getInstance()->creat(owner,id);
						timer[i] = CCGame->m_ParticleData->transmitterGroup[transmitter_ID][i][TRANSMITTER_BULLET_FREQUENCY];
						SetState(ST_FIRE);
						state_fire_timer = 0;
					}
					else
					{
						id = CCGame->m_ParticleData->transmitterGroup[transmitter_ID][i][TRANSMITTER_BULLET_GROUP_ID];
						CParticleList::getInstance()->creat(owner,id);
						timer[i] = CCGame->m_ParticleData->transmitterGroup[transmitter_ID][i][TRANSMITTER_BULLET_FREQUENCY];
						SetState(ST_FIRE);
						state_fire_timer = 0;
					}
				}
				else {
					timer[i]--;
				}
			}
			break;
		case ST_FIRE:
			if ( state_fire_timer++ > 0 ){
				SetState(ST_NONE);
			}
			break;
		}
	}
}